/********************************************************************************
 * UserServiceDelegator.java
 *
 * Created by Ankur Jain
 * Last modified Date: Aug 26, 2008 11:13:51 AM by Ankur
 * Version : 0.1
 * Copyright (c) 2008 - 2009 cyberThink Infotech Pvt. Ltd., All rights reserved.
 ********************************************************************************/

package com.printeverywhere.delegator;

import java.util.Iterator;
import java.util.List;
import java.util.Set;
import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import com.printeverywhere.dto.Announcement;
import com.printeverywhere.util.HibernateUtil;
import com.printeverywhere.util.SearchCriteria;

/**
 * The Class AnnouncementServiceDelegator.
 */
public class AnnouncementServiceDelegator {
	
	/** The log. */
	private static Logger log = Logger.getLogger(AnnouncementServiceDelegator.class);

	/**
	 * Gets the list.
	 * 
	 * @return the list
	 * 
	 * @throws HibernateException the hibernate exception
	 */
	@SuppressWarnings("unchecked")
	public static List<Announcement> getList() throws HibernateException {
		if(log.isInfoEnabled()){
			log.info("Getting list for announcement without search criteria".intern());
		}
		Session session = HibernateUtil.getSession();
		try {
			Transaction transaction = session.beginTransaction();
			List<Announcement> list = session.createCriteria(Announcement.class).list();
			transaction.commit();
			return list;
		} catch (HibernateException e) {
			log.error("Got exception when getting list for announcement without search criteria".intern());
			throw e;
		} finally {
			if(log.isInfoEnabled()){
				log.info("Closing session".intern());
			}
			session.flush();
			session.close();
		}
	}
	
	/**
	 * Gets the announcement.
	 * 
	 * @param id the id
	 * 
	 * @return the announcement
	 * 
	 * @throws HibernateException the hibernate exception
	 */
	public static Announcement getAnnouncement(int id) throws HibernateException {
		if(log.isInfoEnabled()){
			log.info("Getting list for announcement without search criteria".intern());
		}
		Session session = HibernateUtil.getSession();
		try {
			Transaction transaction = session.beginTransaction();
			Criteria criteria = session.createCriteria(Announcement.class);
			criteria.add(Restrictions.eq("id".intern(), id));
			List<Announcement> groupList = criteria.list();
			transaction.commit();
			return (groupList.size() == 0) ? new Announcement() : groupList.get(0);
		} catch (HibernateException e) {
			log.error("Got exception when getting list for announcement without search criteria".intern());
			throw e;
		} finally {
			if(log.isInfoEnabled()){
				log.info("Closing session".intern());
			}
			session.flush();
			session.close();
		}
	}

	/**
	 * Gets the list.
	 * 
	 * @param searchCriteria the search criteria
	 * 
	 * @return the list
	 * 
	 * @throws HibernateException the hibernate exception
	 */
	public static List<Announcement> getList(SearchCriteria searchCriteria)throws HibernateException {
		if(log.isInfoEnabled()){
		log.info("Getting list for announcement with search criteria".intern());
		}
		Session session = HibernateUtil.getSession();
		Criteria criteria = session.createCriteria(Announcement.class);
		String orderBY = searchCriteria.getOrderBy();
		if (orderBY != null) {
			if (searchCriteria.isAsc()) {
				criteria.addOrder(Order.asc(orderBY));
			} else {
				criteria.addOrder(Order.desc(orderBY));
			}
		}
		Set<String> keys = searchCriteria.getAttributeNames();
		Iterator<String> it = keys.iterator();
		String key = null;
		Object value = null;
		while (it.hasNext()) {
			key = it.next();
			value = searchCriteria.getAttribute(key);
			criteria.add(Restrictions.eq(key, value));
		}
		Transaction transaction = session.beginTransaction();
		List<Announcement> list;
		try {
			list = criteria.list();
			transaction.commit();
		} catch (HibernateException e) {
			log.error("Got exception when getting list for announcement with search criteria".intern());
			throw e;
		} finally {
			if(log.isInfoEnabled()){
				log.info("Closing session".intern());
			}
			session.flush();
			HibernateUtil.closeSession();
		}
		return list;
	}
	
	/**
	 * Gets the announcement list starts with digit.
	 * 
	 * @return the announcement list starts with digit
	 * 
	 * @throws HibernateException the hibernate exception
	 */
	public static List<Announcement> getAnnouncementListStartsWithDigit()throws HibernateException {
		if(log.isInfoEnabled()){
		log.info("Getting AnnouncementListStartsWithDigit list for announcement with search criteria".intern());
		}
		Session session = HibernateUtil.getSession();
		Transaction transaction = session.beginTransaction();
		List<Announcement> list;
		try {
			list = session.getNamedQuery("getAnnouncementNotStartWithAlbha".intern()).list();
			transaction.commit();
		} catch (HibernateException e) {
			log.error("Got exception when getting list for announcement with search criteria".intern());
			throw e;
		} finally {
			if(log.isInfoEnabled()){
				log.info("Closing session".intern());
			}
			session.flush();
			HibernateUtil.closeSession();
		}
		return list;
	}
	
	/**
	 * Gets the case insensitive list.
	 * 
	 * @param searchCriteria the search criteria
	 * 
	 * @return the case insensitive list
	 * 
	 * @throws HibernateException the hibernate exception
	 */
	public static List<Announcement> getCaseInsensitiveList(SearchCriteria searchCriteria)throws HibernateException {
		if(log.isInfoEnabled()){
		log.info("Getting case - insensitive list for announcement with search criteria".intern());
		}
		Session session = HibernateUtil.getSession();
		Criteria criteria = session.createCriteria(Announcement.class);
		String orderBY = searchCriteria.getOrderBy();
		if (orderBY != null) {
			if (searchCriteria.isAsc()) {
				criteria.addOrder(Order.asc(orderBY));
			} else {
				criteria.addOrder(Order.desc(orderBY));
			}
		}
		Set<String> keys = searchCriteria.getAttributeNames();
		Iterator<String> it = keys.iterator();
		String key = null;
		Object value = null;
		while (it.hasNext()) {
			key = it.next();
			value = searchCriteria.getAttribute(key);
			criteria.add(Restrictions.ilike(key, value.toString(), MatchMode.START));
		}
		Transaction transaction = session.beginTransaction();
		List<Announcement> list;
		try {
			list = criteria.list();
			transaction.commit();
		} catch (HibernateException e) {
			log.error("Got exception when getting list for announcement with search criteria".intern());
			throw e;
		} finally {
			if(log.isInfoEnabled()){
				log.info("Closing session".intern());
			}
			session.flush();
			HibernateUtil.closeSession();
		}
		return list;
	}
	
	/**
	 * Gets the case insensitive list of search.
	 * 
	 * @param searchCriteria the search criteria
	 * 
	 * @return the case insensitive list of search
	 * 
	 * @throws HibernateException the hibernate exception
	 */
	public static List<Announcement> getCaseInsensitiveListOfSearch(SearchCriteria searchCriteria)throws HibernateException {
		if(log.isInfoEnabled()){
		log.info("Getting case - insensitive list for announcement with search criteria".intern());
		}
		Session session = HibernateUtil.getSession();
		Criteria criteria = session.createCriteria(Announcement.class);
		String orderBY = searchCriteria.getOrderBy();
		if (orderBY != null) {
			if (searchCriteria.isAsc()) {
				criteria.addOrder(Order.asc(orderBY));
			} else {
				criteria.addOrder(Order.desc(orderBY));
			}
		}
		Set<String> keys = searchCriteria.getAttributeNames();
		Iterator<String> it = keys.iterator();
		String key = null;
		Object value = null;
		while (it.hasNext()) {
			key = it.next();
			value = searchCriteria.getAttribute(key);
			criteria.add(Restrictions.ilike(key, value.toString(), MatchMode.ANYWHERE));
		}
		Transaction transaction = session.beginTransaction();
		List<Announcement> list;
		try {
			list = criteria.list();
			transaction.commit();
		} catch (HibernateException e) {
			log.error("Got exception when getting list for announcement with search criteria".intern());
			throw e;
		} finally {
			if(log.isInfoEnabled()){
				log.info("Closing session".intern());
			}
			session.flush();
			HibernateUtil.closeSession();
		}
		return list;
	}

	/**
	 * Insert announcement.
	 * 
	 * @param announcement the announcement
	 * 
	 * @throws HibernateException the hibernate exception
	 */
	public static void insertAnnouncement(Announcement announcement) throws HibernateException {
		if(log.isInfoEnabled()){
			log.info("Called insertAnnouncement method".intern());
		}
		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.saveOrUpdate(announcement);
			tx.commit();
		} catch (RuntimeException e) {
			log.error("Got exception when insert announcement with search criteria".intern());
			if (tx != null)
				tx.rollback();
			throw e;
		} finally {
			if(log.isInfoEnabled()){
				log.info("Closing session".intern());
			}
			session.flush();
			session.close();
		}
	}

	/**
	 * Gets the announcement count.
	 * 
	 * @param searchCriteria the search criteria
	 * 
	 * @return the announcement count
	 * 
	 * @throws HibernateException the hibernate exception
	 */
	public static int getAnnouncementCount(SearchCriteria searchCriteria)throws HibernateException{
		if(log.isInfoEnabled()){
			log.info("Getting announcement count".intern());
		}
		if(searchCriteria == null)searchCriteria = new SearchCriteria();
		Session session = HibernateUtil.getSession();
		Criteria criteria = session.createCriteria(Announcement.class);
		Set<String> keys = searchCriteria.getAttributeNames();
		Iterator<String> it = keys.iterator();
		String key = null;
		Object value = null;
		if(it.hasNext()){
		key = it.next();
		value = searchCriteria.getAttribute(key);
		}
		if(value!=null){
		criteria.add(Restrictions.eq(key, value));
		criteria.setProjection(Projections.count(key.toString()));
		}
		else criteria.setProjection(Projections.rowCount());
		Transaction transaction = session.beginTransaction();
		List list;
		try {
			list = criteria.list();
			transaction.commit();
		} catch (HibernateException e) {
			log.error("Got exception when getting list for announcement with search criteria".intern());
			throw e;
		} finally {
			if(log.isInfoEnabled()){
				log.info("Closing session".intern());
			}
			session.flush();
			HibernateUtil.closeSession();
		}
		if(list.size()>0)
		return (Integer)list.get(0);
		else return 0;
	}
	
	/**
	 * Delete announcement.
	 * 
	 * @param announcement the announcement
	 * 
	 * @throws HibernateException the hibernate exception
	 */
	public static void deleteAnnouncement(Announcement announcement) throws HibernateException {
		if(log.isInfoEnabled()){
			log.info("Called deleteannouncement method".intern());
		}
		Session session = HibernateUtil.getSession();
		Transaction transaction = null;
		try {
			transaction = session.beginTransaction();
			session.delete(announcement);
			transaction.commit();
		} catch (RuntimeException e) {
			log.error("Got exception when Delete announcement".intern());
			if (transaction != null)
				transaction.rollback();
			throw e;
		} finally {
			if(log.isInfoEnabled()){
				log.info("Closing session".intern());
			}
			session.flush();
			session.close();
		}
	}
}